package com.kystar.muxplayer.manager;

import android.content.ClipData;
import android.content.Context;
import android.content.Intent;
import android.view.DragEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.Toast;
import com.kystar.muxplayer.BaseApp;
import com.kystar.muxplayer.base.TLog;
import com.kystar.muxplayer.model.Bound;
import com.kystar.muxplayer.model.Layer;
import com.kystar.muxplayer.model.Source;
import com.kystar.muxplayer.protocol.code.set.CS0E_LayerInfo;
import com.kystar.muxplayer.selfview.PvmFrameLayout;
import com.kystar.muxplayer.selfview.PvmLayerView;
import java.security.PublicKey;

/**
 * Created by peanut on 2018/8/14
 */
public class PvmManager implements View.OnDragListener {
    private static final String TAG = "PvmManager";
    Context mContext;
    View[] mViews;
    private static PvmManager sMPvmManager;
    private PvmFrameLayout mFrameLayout;
    Layer[] mLayers;

    public static PvmManager getPvmSingleInstance() {
        if (sMPvmManager == null) {
            sMPvmManager = new PvmManager();
        }
        return sMPvmManager;
    }

    public PvmFrameLayout getFrameLayout() {
        return mFrameLayout;
    }


    public void init(PvmFrameLayout frameLayout) {
        mContext = frameLayout.getContext();
        mFrameLayout = frameLayout;
        Layer[] layers = BaseApp.getSettings().getPVWConfig().getLayers();
        mViews = new View[layers.length];
        mFrameLayout.post(new Runnable() {
            @Override
            public void run() {
                int frameWidth = frameLayout.getWidth();
                int frameHeight = frameLayout.getHeight();
                for (int i = 0; i < layers.length; i++) {
                    Layer layer = layers[i];
                    View view = new View(mContext);

                    if (layer.isVisible()) {
                        Bound viewBound = BaseApp.toLocal(layer.getViewBound(),frameWidth,frameHeight);
                        FrameLayout.LayoutParams params = new LayoutParams(viewBound.w,viewBound.h);
                        params.leftMargin = viewBound.x;
                        params.topMargin = viewBound.y;
                        view.setLayoutParams(params);
//                setText(String.format("%4s:%s\nPos :%d.%d\nSize:%dx%d",layer.getLayerType().name,layer.getSourceType().name,viewBound.x,viewBound.y,viewBound.w,viewBound.h));
//                TLog.e(getText());
                        view.setVisibility(View.VISIBLE);

                    } else {
                        view.setVisibility(View.INVISIBLE);
                        view.setLayoutParams(new LayoutParams(0,0));
                    }
                    view.setTag(layer);
                    mViews[i] = view;
                    frameLayout.addView(view);
                    view.setOnDragListener(PvmManager.this);

                }
            }
        });

    }

    @Override
    public boolean onDrag(View v, DragEvent event) {
        if (v.getVisibility()!=View.VISIBLE) return false;
        int action = event.getAction();
        Layer layer = (Layer) v.getTag();
        if (action!=2)
        TLog.e(layer.getLayerType(),action);
        switch (action) {
            case DragEvent.ACTION_DRAG_STARTED:
                break;
            case DragEvent.ACTION_DRAG_ENDED:
                break;
            case DragEvent.ACTION_DRAG_ENTERED:
                mFrameLayout.setSelectView(v);
                break;
            case DragEvent.ACTION_DRAG_EXITED:
                mFrameLayout.setSelectView(null);
                break;
            case DragEvent.ACTION_DRAG_LOCATION:

                break;
            case DragEvent.ACTION_DROP:
                View view = mFrameLayout.getSelectView();
                if (view!=null && view == v) {
//                    Layer layer = (Layer) v.getTag();
                    ClipData clipData =  event.getClipData();
                    Intent intent = clipData.getItemAt(0).getIntent();
//                    Source source = (Source) intent.getParcelableExtra("source");
//                    Source source = (Source) intent.getSerializableExtra("source");
                    int index = intent.getIntExtra("index",0);
                    Source source = BaseApp.getSettings().getSources()[index];
                    switchSource(view,source);
                }

                break;
        }
        return true;
    }

    public void update(Layer[] layers) {
        for (int i = 0; i < 4; i++) {
            View view = mFrameLayout.getChildAt(i);
            Layer layer = (Layer) view.getTag();
            Layer newLayer = layers[layer.getLayerType().ordinal()];
            view.setTag(newLayer);
            layer = newLayer;

            if (layer.isVisible()) {
                Bound viewBound = BaseApp.toLocal(layer.getViewBound(),mFrameLayout.getWidth(),mFrameLayout.getHeight());
                FrameLayout.LayoutParams params = new LayoutParams(viewBound.w,viewBound.h);
                params.leftMargin = viewBound.x;
                params.topMargin = viewBound.y;
                view.setLayoutParams(params);
//                setText(String.format("%4s:%s\nPos :%d.%d\nSize:%dx%d",layer.getLayerType().name,layer.getSourceType().name,viewBound.x,viewBound.y,viewBound.w,viewBound.h));
//                TLog.e(getText());
                view.setVisibility(View.VISIBLE);

            } else {
                view.setVisibility(View.INVISIBLE);
                view.setLayoutParams(new LayoutParams(0,0));
            }
        }

    }

    public void switchSource(View view,Source source) {
        Layer layer = (Layer) view.getTag();
        com.kystar.muxplayer.ui.Toast.show("test"+source.getType().name+ layer.getLayerType().name);
        BaseApp.send(CS0E_LayerInfo.switchLayer(layer,source)).subscribe(cmd->{

        },e->{},()->{
            layer.setSourceType(source.getType());
            mFrameLayout.invalidate();
        });
    }
}
